// pages/me/income/income.js
import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
import { AccurateMath } from '../../../utils/accurateMath.js'
import { TimeToDate } from '../../../utils/timeToDate.js'
let timeToDate = new TimeToDate()
let http = new Http()
let accurateMath = new AccurateMath();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: null,
    select: 0,
    withdraw: 0,
    showWithdraw: 0,
    withdrawSuccess: 0,
    windowHeight: 0,
    scrollViewHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user: JSON.parse(options.user),
      wPageNum: 1,
      wPageSize: 6,
      wPages: 1,
      withdraws: [],
      iPageNum: 1,
      iPageSize: 6,
      iPages: 1,
      incomes: []
    })
    this.setScrollViewHeight()
    this.getRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 自定义方法
   */
  selectNav: function (e) {
    var select = e.currentTarget.dataset.select
    this.setData({
      select: select,
      wPageNum: 1,
      wPageSize: 6,
      wPages: 1,
      withdraws: [],
      iPageNum: 1,
      iPageSize: 6,
      iPages: 1,
      incomes: []
    })
    this.getRecord()
  },

  showWithdraw: function () {
    if (this.data.user.money > 0) {
      var that = this
      http.sendHttp(Api.get_user_status, {
        data: {},
        success: function (res) {
          if (res.data.obj.isBindIdCard == 1) {
            that.setData({
              showWithdraw: 1
            })
          } else {
            wx.navigateTo({
              url: '/pages/me/bindIdCard/bindIdCard',
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '可提现余额不足',
        icon: 'none'
      })
    }
  },
  closeWithdraw: function () {
    this.setData({
      showWithdraw: 0,
      withdrawSuccess: 0
    })
  },

  withdraw: function () {
    var withdraw = this.data.withdraw
    var user = this.data.user
    var that = this
    if (withdraw > 0) {
      if (withdraw <= user.money) {
        http.sendHttp(Api.distribute_withdraw, {
          data: {
            amount: withdraw
          },
          success: function (res) {
            user.money = accurateMath.accSub(user.money, withdraw)
            that.setData({
              withdrawSuccess: 1,
              user: user
            })
          }
        }, 'POST')
      } else {
        wx.showToast({
          title: '可提现余额不足',
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none'
      })
    }
  },

  withdrawSuccess: function () {
    this.setData({
      showWithdraw: 0,
      withdrawSuccess: 0
    })
  },

  setScrollViewHeight: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      }
    })
    let query = wx.createSelectorQuery().in(this)
    query.select('.container').boundingClientRect()
    query.select('.navigation').boundingClientRect()
    query.exec((res) => {
      let containerHeight = res[0].height
      let navigationHeight = res[1].height
      let scrollViewHeight = this.data.windowHeight - containerHeight - navigationHeight - 30
      this.setData({
        scrollViewHeight: scrollViewHeight
      })
    })
  },

  bindinput: function (e) {
    var value = e.detail.value
    this.setData({
      withdraw: value
    })
  },

  bindScrollTolower: function () {
    this.getRecord()
  },

  getRecord: function () {
    if (this.data.select == 0) {
      this.getDistributeIncome()
    } else {
      this.getDistributeWithdraw()
    }
  },
  getDistributeWithdraw: function () {
    var that = this
    var pageNum = this.data.wPageNum
    var pageSize = this.data.wPageSize
    var pages = this.data.wPages
    if (pageNum <= pages) {
      http.sendHttp(Api.get_distribute_withdraw, {
        data: {
          pageNum: pageNum,
          pageSize: pageSize
        },
        success: function (res) {
          var withdraws = res.data.obj.withdraws
          for (var index in withdraws) {
            withdraws[index].tmCreate = timeToDate.formatTime(withdraws[index].tmCreate / 1000, 'Y-M-D h:m')
          }
          that.setData({
            withdraws: that.data.withdraws.concat(withdraws),
            wPages: res.data.obj.pages,
            wPageNum: pageNum + 1
          })
        }
      })
    }
  },
  getDistributeIncome: function () {
    var that = this
    var pageNum = this.data.iPageNum
    var pageSize = this.data.iPageSize
    var pages = this.data.iPages
    if (pageNum <= pages) {
      http.sendHttp(Api.get_distribute_income, {
        data: {
          pageNum: pageNum,
          pageSize: pageSize
        },
        success: function (res) {
          var incomes = res.data.obj.incomes
          for (var index in incomes) {
            incomes[index].tmCreate = timeToDate.formatTime(incomes[index].tmCreate / 1000, 'Y-M-D h:m')
            if (incomes[index].goods.title.length > 14) {
              incomes[index].goods.title = incomes[index].goods.title.substring(0, 14) + '...'
            }
          }
          that.setData({
            incomes: that.data.incomes.concat(incomes),
            iPages: res.data.obj.pages,
            iPageNum: pageNum + 1
          })
        }
      })
    }
  },

  doNothing: function () {
    
  }
})